# syntax=docker/dockerfile:1

# ---- Stage 1: build the embedded Vue panel ----
# vite.config.js writes the build to ../internal/api/dist, i.e. /internal/api/dist
# here, which the Go binary embeds via //go:embed all:dist.
FROM node:22-alpine AS panel
WORKDIR /panel
COPY panel/package.json panel/package-lock.json ./
RUN npm ci
COPY panel/ ./
RUN npm run build

# ---- Stage 2: build the static Go binary (panel embedded) ----
FROM golang:1.26-alpine AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
# Overlay the freshly built panel so //go:embed all:dist picks it up.
COPY --from=panel /internal/api/dist ./internal/api/dist
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" \
    -o /out/api-server ./cmd/server

# ---- Stage 3: minimal runtime ----
FROM alpine:latest
RUN apk add --no-cache ca-certificates tzdata \
    && adduser -D -u 10001 app
WORKDIR /app
COPY --from=build /out/api-server ./api-server
USER app
# Default listen address (override with API_ADDR). PocketBase URL, CORS origins,
# and the optional POCKETBASE_ADMIN_* service account come from env at runtime.
ENV API_ADDR=:8080
EXPOSE 8080
ENTRYPOINT ["/app/api-server"]
